﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Task.Data;
using CentralOffice.Task.BusinessActions;

using ApprovalTable = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsDataTable;
using ApprovalRow = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsRow;
using System.Collections.Specialized;

namespace CentralOffice.WebClient.Tasks
{
    public partial class TimeoutApprovals : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlCategory, SiteConstants.ApprovalObject, true);
                ddlCategory.SelectedIndex = 0;
            }
        }

        protected void odsApproval_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //int status = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "通过");

            IOrderedDictionary parameters = e.InputParameters;
            if (chkIsTimeout.Checked)
            {
                if (parameters["whereClause"] == null)
                {
                    // ((WarningDate < DateApproved) OR (DateApproved IS NULL AND WarningDate < GETDATE())
                    //parameters["whereClause"] = " AND ApplicationIsDeleted = 0 AND ElapsedTime > " + SiteUtil.GetDefaultWarningDate().ToString();

                    parameters["whereClause"] = " AND ApplicationIsDeleted = 0 AND ((WarningDate < DateApproved) OR (DateApproved IS NULL AND WarningDate < GETDATE())) " ;
                }
                else
                {
                    if (!parameters["whereClause"].ToString().Trim().StartsWith("AND ApplicationIsDeleted = 0"))
                    {
                        parameters["whereClause"] = " AND ApplicationIsDeleted = 0 AND ((WarningDate < DateApproved) OR (DateApproved IS NULL AND WarningDate < GETDATE())) " + parameters["whereClause"].ToString();
                    }
                }
            }

            //ApprovalRow approval = (new ApprovalDataSet()).Approvals.NewApprovalsRow();
            //approval.ApprovedBy = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
            //parameters["whereClause"] = task.FilterExpression;
            
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            ApprovalRow approval = (new ApprovalDataSet()).Approvals.NewApprovalsRow();

            approval.Name = txtName.Text;
            if (ddlCategory.SelectedIndex > 0)
            {
                approval.Category = Int32.Parse(ddlCategory.SelectedItem.Value);
            }
            approval.Applicant = txtApplicant.Text;

            if (txtApprovedBy.Text.Length > 0)
            {
                approval.ApprovedBy = txtApprovedBy.Text;
            }

            if (txtDateOfApplication.Text.Length > 0)
            {
                approval.DateOfApplication = DateTime.Parse(txtDateOfApplication.Text);
            }
            if (txtDateOfApplicationEnd.Text.Length > 0)
            {
                approval.DateOfApplicationEnd = DateTime.Parse(txtDateOfApplicationEnd.Text);
            }

            //string where = approval.FilterExpression;
            //if (chkIsTimeout.Checked)
            //{
            //    where += " AND ElapsedTime > " + SiteUtil.GetDefaultWarningDate().ToString();
            //}

            odsApproval.SelectParameters.Clear();
            odsApproval.SelectParameters.Add("whereClause", approval.FilterExpression);
            
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApprovalRow approval = ((DataRowView)e.Row.DataItem).Row as ApprovalRow;

                if (!approval.IsDateApprovedNull())
                {
                    if (approval.ResultApproved)
                    {
                        e.Row.Cells[7].Text = "通过";
                    }
                    else
                    {
                        e.Row.Cells[7].Text = "<span class=\"warning\">否决</span>";
                    }
                }

                //SiteUtil.SetElapsedTime(e, 7, approval.IsElapsedTimeNull() ? 0 : approval.ElapsedTime);
                //e.Row.Cells[8].Text = SiteUtil.GetDateTimeString((approval.IsDateApprovedNull() ? DateTime.Now : approval.DateApproved) - approval.CreatedTime);
                DateTime approvedDate = approval.IsDateApprovedNull() ? DateTime.Now : approval.DateApproved;
                e.Row.Cells[8].Text = SiteUtil.GetDateTimeString(approvedDate - approval.CreatedTime, (approval.IsWarningDateNull() ? DateTime.Now : approval.WarningDate) < approvedDate);
            }
        }

    }
}
